package shmallarchitect.web.pages;

import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import shmallarchitect.faces.util.GalleryUtil;
import shmallarchitect.jpa.entities.Gallery;
import shmallarchitect.jpa.entities.Owner;
import shmallarchitect.jpa.entities.User;

/**
 *
 * @author jaccus
 */
public class CreateGallery {

    private HtmlInputText name;
    private HtmlInputText owner;
    private HtmlInputText ownerEmail;
    private HtmlInputText country;
    private HtmlInputText city;
    private HtmlInputText zip;
    private HtmlInputText street;
    private HtmlInputText number;

    public String createGallery() {

        Gallery newGallery = new Gallery();
        newGallery.setName(name.getValue().toString());
        Owner newOwner = new Owner();
        newOwner.setName(owner.getValue().toString());
        newOwner.setEmail(ownerEmail.getValue().toString());
        newGallery.setOwner(newOwner); //TODO: owner for gallery is just a workaround
        newGallery.setCountry(country.getValue().toString());
        newGallery.setCity(city.getValue().toString());
        newGallery.setZip(zip.getValue().toString());
        newGallery.setStreet(street.getValue().toString());
        newGallery.setNumber(number.getValue().toString());
        newGallery.setUser((User) JSFUtil.getManagedObject("UserBean"));

        final String newGalleryName = GalleryUtil.addNewGallery(newGallery);

        if (newGalleryName == null) { // gallery exists

            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage("Gallery " + newGalleryName + " already exists! Please choose another name."));
            newGallery = null;

            return (null);
        } else {
            List<Gallery> galleries = (List<Gallery>) JSFUtil.getManagedObject("galleries");
            galleries.add(newGallery);

            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
                    "Gallery " + newGalleryName + " created successfully!"));
            System.out.println("Done Creating Gallery..");
            return ("success");
        }
    }

    public HtmlInputText getCity() {
        return city;
    }

    public void setCity(HtmlInputText city) {
        this.city = city;
    }

    public HtmlInputText getCountry() {
        return country;
    }

    public void setCountry(HtmlInputText country) {
        this.country = country;
    }

    public HtmlInputText getName() {
        return name;
    }

    public void setName(HtmlInputText name) {
        this.name = name;
    }

    public HtmlInputText getNumber() {
        return number;
    }

    public void setNumber(HtmlInputText number) {
        this.number = number;
    }

    public HtmlInputText getOwner() {
        return owner;
    }

    public void setOwner(HtmlInputText owner) {
        this.owner = owner;
    }

    public HtmlInputText getStreet() {
        return street;
    }

    public void setStreet(HtmlInputText street) {
        this.street = street;
    }

    public HtmlInputText getZip() {
        return zip;
    }

    public void setZip(HtmlInputText zip) {
        this.zip = zip;
    }

    public HtmlInputText getOwnerEmail() {
        return ownerEmail;
    }

    public void setOwnerEmail(HtmlInputText ownerEmail) {
        this.ownerEmail = ownerEmail;
    }

}
